<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="cashNo">
              <el-input v-model="filters.cashNo" clearable placeholder="提现编号"></el-input>
            </el-form-item>
            <el-form-item prop="cashState">
              <el-select v-model="filters.cashState" clearable placeholder="提现状态">
                <el-option v-for="item in cashStateList" :value="item.id" :label="item.name" :key="item.id"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="businessName">
              <el-input v-model="filters.businessName" placeholder="商户名称"> </el-input>
            </el-form-item>
            <el-form-item prop="userName">
              <el-input v-model="filters.userName" placeholder="用户名称"> </el-input>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table ref="tableList" :data="dataList" :fit="true" highlight-current-row v-loading="listLoading" element-loading-text="数据加载中，请稍后..." empty-text="对不起，没有找到相关数据！">
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column align="center" label="提现编号" prop="cashNo" width="240"> </el-table-column>
        <el-table-column align="center" label="用户昵称" prop="userName" width="150"> </el-table-column>
        <el-table-column align="center" label="商户名称" prop="businessName" width="150"> </el-table-column>
        <el-table-column align="center" label="提现金额(元)" prop="amount" width="150"> </el-table-column>
        <el-table-column align="center" label="提现日期" prop="createDate" width="140"> </el-table-column>
        <el-table-column label="提现状态" align="center" width="140">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="formatCashStates(scope.row.cashState)">
                {{ formatCashState(scope.row.cashState) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="打款人" prop="paymentUserName" width="140"> </el-table-column>
        <el-table-column align="center" label="打款时间" prop="paymentDate" width="150"> </el-table-column>
        <el-table-column align="center" label="内部备注" prop="insideRemark" width="150"> </el-table-column>
        <el-table-column label="操作" width="136" fixed="right">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionExamine" @click.native="handleClickExamine(scope.row)">审核 </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>

    <el-dialog class="examine" title="审核" :visible.sync="examine.show" width="fit-content" center>
      <div class="examine-item">
        <div class="label">提现状态：</div>
        <el-select v-model="examine.cashState">
          <el-option v-for="item in cashStateList" :value="item.id" :label="item.name" :key="item.id"> </el-option>
        </el-select>
      </div>
      <div class="examine-item">
        <div class="label">内部备注：</div>
        <el-input type="textarea" v-model="examine.insideRemark"> </el-input>
      </div>
      <div slot="footer">
        <el-button type="primary" @click="confirmExamine"> 确认 </el-button>
        <el-button @click="examine.show = false"> 取消 </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import * as message from '@/constants/message';
import CashRecordApi from '@/api/finance/cashRecord';
import BusinessApi from '@/api/business/business';
export default {
  name: 'CashRecordManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      listLoading: true,
      filters: {
        cashNo: '',
        cashState: '',
        businessName: '',
        userName: '',
      },
      dataList: [],
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      businessList: [],
      cashStateList: [
        { id: 1, name: '提现中' },
        { id: 5, name: '已提现' },
        { id: 10, name: '用户告知未到账' },
        { id: 15, name: '提现失败' },
        { id: 20, name: '提现成功' },
      ],

      permissionCheck: 'SYSTEM_MANAGER_CASH_RECORD_LIST_SEE',
      permissionExamine: 'SYSTEM_MANAGER_CASH_RECORD_LIST_EXAMINE',
      examine: {
        show: false,
        cashState: '',
        insideRemark: '',
      },
      cashRecordId: '',
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  components: {},
  watch: {},
  methods: {
    init() {
      this.listLoading = true;
      this.listData();
      this.getTableHeight();
      this.getBusinessList();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode === 13) {
        this.onSearch();
      }
    },
    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    // 获取商户列表
    getBusinessList() {
      BusinessApi.listIdNameVo().then((res) => {
        this.businessList = res.data;
      });
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listData();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listData();
    },
    handleClickDetail(row) {
      this.$router.push({
        path: '/finance/cashRecordDetail',
        query: { id: row.cashRecordId },
      });
    },
    handleClickExamine(row) {
      const { cashRecordId, cashState } = row;
      const { examine } = this;
      this.cashRecordId = cashRecordId;
      examine.show = true;
      examine.cashState = cashState;
    },
    confirmExamine() {
      const { examine, cashRecordId } = this;

      const params = {
        id: cashRecordId,
        state: examine.cashState,
        content: examine.insideRemark,
      };
      this.$confirm('确定提交', '提示', {}).then(() => {
        CashRecordApi.recordOperation(params).then(() => {
          examine.show = false;
          this.listData();
          this.$Utils.notifySuccess();
        });
      });
    },
    formatCashState(cellValue) {
      if (cellValue === 1) {
        return '提现中';
      } else if (cellValue === 5) {
        return '已提现';
      } else if (cellValue === 10) {
        return '用户告知未到账';
      } else if (cellValue === 15) {
        return '提现失败';
      } else if (cellValue === 20) {
        return '提现成功';
      }
    },
    formatCashStates(cellValue) {
      if (cellValue === 1) {
        return '';
      } else if (cellValue === 5) {
        return 'success';
      } else if (cellValue === 10) {
        return 'warning';
      } else if (cellValue === 15) {
        return 'danger';
      } else if (cellValue === 20) {
        return 'success';
      }
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      // 获取列表数据
      let cond = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        cashNo: this.filters.cashNo,
        cashState: this.filters.cashState,
        userName: this.filters.userName,
        businessName: this.filters.businessName,
      };
      this.listLoading = true;
      CashRecordApi.listCashRecord(cond)
        .then((res) => {
          this.dataList = res.data.list;
          this.page.pageTotal = res.data.total;
          this.$refs.tableList.bodyWrapper.scrollTop = 0;
        })
        .finally(() => {
          this.$refs.tableList.doLayout();
          this.listLoading = false;
        });
    },
  },
};
</script>
<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
<style lang="scss" scoped>
.examine {
  ::v-deep .el-dialog__body {
    padding: 60px;
    .examine-item {
      display: flex;
      align-items: center;
      &:first-of-type {
        margin-bottom: 50px;
      }
      .label {
        width: 130px;
      }
      .el-textarea,
      .el-select {
        width: 240px;
      }
    }
  }
}
</style>
